package com.example.ycdyus.dailyfruit.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ycdyus.dailyfruit.R;
import com.example.ycdyus.dailyfruit.adapter.BaikeGridViewAdapter;
import com.example.ycdyus.dailyfruit.adapter.BaikeListViewAdapter;
import com.example.ycdyus.dailyfruit.bean.BaikeGridBean;
import com.example.ycdyus.dailyfruit.bean.BaikeListBean;
import com.example.ycdyus.dailyfruit.bean.BaikeListEntity;
import com.example.ycdyus.dailyfruit.bean.ShopBean;
import com.example.ycdyus.dailyfruit.constant.URLConstant;
import com.example.ycdyus.dailyfruit.ui.BaikeDetailsActivity;
import com.example.ycdyus.dailyfruit.ui.BaikeDetailsActivity_;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.HttpHandler;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
@EFragment(R.layout.fragment_baike)
public class BaikeFragment extends Fragment {
    @ViewById
    PullToRefreshListView guoshiBaikePulltorefresh;

    private List<BaikeListBean> list = new ArrayList<>();
    private BaikeListViewAdapter adapter;

    private HttpHandler handler,handler1;

    /**
     * 头部
     */
    private TextView search;
    private GridView gv;
    private BaikeGridViewAdapter baikeGridViewAdapter;
    private List<BaikeGridBean> baikeGridBeanList = new ArrayList<>();



    @AfterViews
    public void initData(){

        View view = getActivity().getLayoutInflater().inflate(R.layout.guoshi_baike_headview,null);
        search = (TextView) view.findViewById(R.id.tv_guoshi_baike_search);
        gv = (GridView) view.findViewById(R.id.gv_guoshi_baike_head);
        baikeGridViewAdapter = new BaikeGridViewAdapter(getActivity(),baikeGridBeanList,R.layout.item_guoshi_grid_head);
        gv.setAdapter(baikeGridViewAdapter);

        adapter = new BaikeListViewAdapter(getActivity(),list,R.layout.item_guoshi_baike_listview);
        guoshiBaikePulltorefresh.setAdapter(adapter);


        HttpUtils httpUtils = new HttpUtils();
        handler = httpUtils.send(HttpRequest.HttpMethod.POST, URLConstant.MAIN_GUOSHI_BAIKE_HEAD_POST, getRequestParamsGrid(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    baikeGridBeanList.addAll(JSONArray.parseArray(responseInfo.result, BaikeGridBean.class));
                    baikeGridViewAdapter.notifyDataSetChanged();
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("HHHH", s);
            }
        });
        handler1 = httpUtils.send(HttpRequest.HttpMethod.POST, URLConstant.MAIN_GUOSHI_BAIKE_HEAD_POST, getRequestParamsList(), new RequestCallBack<String>() {
            @Override
            public void onSuccess(ResponseInfo<String> responseInfo) {
                if (responseInfo != null) {
                    BaikeListEntity bean = JSONObject.parseObject(responseInfo.result,BaikeListEntity.class);
                    if(bean!=null&&bean.getMain()!=null){
                        list.addAll(bean.getMain());
                        adapter.notifyDataSetChanged();
                    }
                }
            }

            @Override
            public void onFailure(HttpException e, String s) {
                Log.e("HHHH", s);
            }
        });

        guoshiBaikePulltorefresh.getRefreshableView().addHeaderView(view);


        gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), BaikeDetailsActivity_.class);
                startActivity(intent);
            }
        });

    }

    private RequestParams getRequestParamsGrid(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign", "2a60b2768ce6dced38b414197ac79929");
        params.addBodyParameter("timestamp", "1445429278");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "161c2ae532723d329ee711d284a1b36e");
        params.addBodyParameter("region_id", "106092");
        params.addBodyParameter("service", "bake.getSectionList");
        params.addBodyParameter("device_id", "1b8ba62bf241cee5b8c317d07c0c503e");
        params.addBodyParameter("channel", "portal");
        params.addBodyParameter("version", "3.0.3");
        return params;
    }
    private RequestParams getRequestParamsList(){
        RequestParams params = new RequestParams();
        params.addBodyParameter("sign", "08260bad95abac58a1077e3101166db1");
        params.addBodyParameter("limit", "20");
        params.addBodyParameter("timestamp", "1445429278");
        params.addBodyParameter("platform", "ANDROID");
        params.addBodyParameter("source", "app");
        params.addBodyParameter("connect_id", "161c2ae532723d329ee711d284a1b36e");
        params.addBodyParameter("region_id", "106092");
        params.addBodyParameter("service", "bake.getArticleList");
        params.addBodyParameter("device_id", "1b8ba62bf241cee5b8c317d07c0c503e");
        params.addBodyParameter("page", "1");
        params.addBodyParameter("channel", "portal");
        params.addBodyParameter("version", "3.0.3");
        return params;
    }

    @Override
    public void onStop() {
        super.onStop();
        if(handler!=null){
            handler.cancel();
        }
        if(handler1!=null){
            handler1.cancel();
        }
    }
}
